<?php
/**
 * admin service file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/03
 * Time: 15:20
 */

namespace Modules\Admin\Service;

use ClassesWithParents\E;
use HiCommon\Repository\ShopRepository;
use HiCommon\Repository\ShopImgRepository;
use HiCommon\Repository\ShopModifyRepository;
use HiCommon\Repository\ShopServerTypeRepository;

class ShopModifyService
{


    /**
     *  获取生气变更的列表
     *  2019 11 05
     *
     **/
    public function get_list( array $arr_search ){
        try{
            $arr_option['filed'] = ['id','shop_id'];
            $arr_option['with'] = ['shop_info'=>function( $query ){
                $query->select('id','cate','name','company_name','phone','name_boss');
            }];
            $arr_list = ShopModifyRepository::get_list( (array) $arr_option ,(array) $arr_search );
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data( $arr_list ,$arr_search);
            return hello_success('成功',$arr_list);
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  变更信息详细信息
     *  hinq 2019 11 05
     *
     **/
    public function detail( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            //获取老的商户信息
//            $arr_option['with'] = ['shop_info','province_name','city_name','area_name','shop_info.province_name','shop_info.city_name','shop_info.area_name'];
            $arr_option['with'] = ['shop_info'=>function( $query ){
                $query->select('id','cate','name','company_name','phone','name_boss','logo','types','province','city','area','address','created_at','img','status');
            },'province_name','city_name','area_name','shop_info.province_name','shop_info.city_name','shop_info.area_name','shop_modify_img'=>function($query){
                $query->select('id','shop_modify_id','img');
            },'shop_info.shop_img'=>function($query){
                $query->select('id','shop_id','img');
            }];
            $arr_list = ShopModifyRepository::get_one( (int) $nId, (array) $arr_option )->toArray();

            //获取申请信息 商户选择的服务类型
            $shop_info_types = explode(',',$arr_list['shop_info']['types']);
            $types = explode(',',$arr_list['types']);
            $type_ids = array_merge( $shop_info_types , $types );

            $arr_option2['field'] = ['id','name'];
            $types_info = ShopServerTypeRepository::get_whereIn_list( (array) $type_ids  ,(array) $arr_option2 )->toArray();
            $types1 = [];
            $types2 = [];
            foreach ( $types_info as $item ){
                if( in_array( $item['id'] , $types ) ){
                    $types1[] = $item;
                }
                if( in_array( $item['id'] , $shop_info_types ) ){
                    $types2[] = $item;
                }
            }
            $arr_list['shop_info']['types'] = $types2;
            $arr_list['types'] = $types1;
//            dd( $arr_list);
            return hello_success('成功',$arr_list);
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  审核 变更信息
     *  hinq 2019 11 05
     *
     **/
    public function shenhe( array $arr_input ){
        \DB::beginTransaction();
        try{
            // type =>  2:审核通过   3：驳回申请
            $type = empty( $arr_input['type'] ) ? 2 : $arr_input['type'];
            if( empty( $arr_input['remark'] ) ){
                return hello_error('请填写审核备注');
            }
            if( empty( $arr_input['id'] ) ){
                return hello_error('参数错误');
            }
            //验证当前是否可以审核
            $where = ['status'=>1,'id'=>$arr_input['id']];
            $arr_option['with'] = ['shop_modify_img'];
            $info = ShopModifyRepository::get_one_by_where( (array) $where ,(array) $arr_option );
            if( empty( $info->first() ) ){
                return hello_error('操作失败');
            }else{
                $info = $info->toArray();
            }

            if( $type == 2 ){   //审核通过操作
                $res = self::pass_shenhe( (array) $arr_input , (array) $info );
            }else if( $type == 3 ){   //驳回申请操作
                $res = self::down_shenhe( (array) $arr_input , (array) $info );
            }
            //审核成功后需要更新shop_user
            if( $res['state'] == 0 ){
                \DB::commit();
            }
            return $res;
        }catch (\Exception $exception){
            \DB::rollBack();
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    //驳回申请操作
    public function down_shenhe( array $arr_input  ){

        //修改变更信息
        $modify_data['id'] = $arr_input['id'];
        $modify_data['status'] = 3;
        $modify_data['remark'] = $arr_input['remark'];

        $res = ShopModifyRepository::edit_one( (array) $modify_data );
        if ( !$res ) {
            return hello_error('操作失败');
        }
        return hello_success('成功');
    }



    //审核通过操作
    public function pass_shenhe( array $arr_input , array $info ){

        //修改变更信息
        $modify_data['id'] = $arr_input['id'];
        $modify_data['status'] = 2;
        $modify_data['remark'] = $arr_input['remark'];
        $res = ShopModifyRepository::edit_one( (array) $modify_data );

        //将变更的信息同步到门店表中
        $shop_data['id'] = $info['shop_id'];
        $shop_data['company_name'] = $info['company_name'];
        $shop_data['province'] = $info['province'];
        $shop_data['city'] = $info['city'];
        $shop_data['area'] = $info['area'];
        $shop_data['address'] = $info['address'];
        $shop_data['name_boss'] = $info['name_boss'];
        $shop_data['phone'] = $info['phone'];
        $shop_data['img'] = $info['img']['src'];
        $res2 = ShopRepository::edit_one( (array) $shop_data );

        $count = ShopImgRepository::get_count_by_where( (array) ['shop_id'=>$info['shop_id']] );
        $res3 = true;
        if( $count > 0 ){
            //删除门店之前的照片
            $res3 = ShopImgRepository::delete_where_list( (array) ['shop_id'=>$info['shop_id']]);
        }
        $res4 = true;
        //同步门店的照片
        if( count( $info['shop_modify_img'] ) > 0 ){
            $shop_temp['shop_id'] = $info['shop_id'];
            foreach ( $info['shop_modify_img'] as $item ){
                $shop_temp['img'] = $item['img']['src'];
                $res4 = ShopImgRepository::add_one((array) $shop_temp );
            }
        }
        if ( $res &&  $res2 && $res3 && $res4) {
            return hello_success('成功');
        }
        return hello_error('操作失败');
    }





}